package com.qingke.p2;

import java.util.Arrays;

public class StringArray {
	public static void main(String[] args){
		String[] str={"","f","","f","a","ss","b","JQ","b","fv","a","ss"};
		str=deleteJQ(str);
		System.out.println(Arrays.toString(str));
		String[][] arr=splitArr(str);
		System.out.println(Arrays.deepToString(arr));
		arr=reverseArr(arr);
		System.out.println(Arrays.deepToString(arr));
		arr=deDuplication(arr);
		System.out.println(Arrays.deepToString(arr));
	}
	
	public static String[] deleteJQ(String[] arr){
		int count=0;
		for(int i=0;i<arr.length;i++){
			if(arr[i].contains("JQ"))
				count++;
		}
		String[] arrs=new String[arr.length-count];
		for(int i=0,j=0;i<arr.length;i++){
			if(!arr[i].contains("JQ")){
				arrs[j]=arr[i];
				j++;
			}
		}
		return arrs;
	}
	
	public static String[][] splitArr(String[] arr){
		String[][] arrs = new String[(arr.length+1)/2][2];
		for(int i=0;i<arrs.length-1;i++){
			arrs[i][0]=arr[2*i];
			arrs[i][1]=arr[2*i+1];
		}
		if(arr.length%2==0){
			arrs[arrs.length-1][0]=arr[arr.length-2];
			arrs[arrs.length-1][1]=arr[arr.length-1];
		}
		else{
			arrs[arrs.length-1][0]=arr[arr.length-1];
			arrs[arrs.length-1][1]="";
		}
		return arrs;
	}
	
	public static String[][] reverseArr(String[][] arr){
		String str;
		String[] strArr;
		for(int i=0;i<arr.length/2;i++){
			strArr=arr[i];
			arr[i]=arr[arr.length-1-i];
			arr[arr.length-1-i]=strArr;
		}
		for(int i=0;i<arr.length;i++){
			str=arr[i][0];
			arr[i][0]=arr[i][1];
			arr[i][1]=str;
		}
		return arr;
	}
	
	public static String[][] deDuplication(String[][] arr){
		String[][] strArr=arr;
//		String[][] strArr=new String[arr.length][2];
//		for(int i=0;i<arr.length;i++){
//			for(int j=0;j<2;j++){
//				strArr[i][j]=arr[i][j];
//			}
//		}
		
		for(int i=0;i<arr.length;i++){
			strArr[i]=arr[i];
		}
		for(int i=0;i<arr.length;i++){
			for(int j=0;j<2;j++){
				for(int k=0;k<arr.length;k++){
					for(int l=0;l<2;l++){
						if(!(i==k&&j==l)&&strArr[i][j]==strArr[k][l]){
							strArr[k][l]=null;
						}
					}
				}
			}
		}
		int count=0;
		for(int i=0;i<strArr.length;i++){
			if(strArr[i][0]==null&&strArr[i][1]==null)
				count++;
		}
		String[][] newArr=new String[arr.length-count][];
		for(int i=0,j=0;i<strArr.length;i++){
			if(strArr[i][0]!=null&&strArr[i][1]!=null){
				newArr[j]=strArr[i];
				j++;
			}
			else if(strArr[i][0]!=null){
				newArr[j]=new String[]{strArr[i][0]};
				j++;
			}
			else if(strArr[i][1]!=null){
				newArr[j]=new String[]{strArr[i][1]};
				j++;
			}
		}
		return newArr;
//		return strArr;
	}
}
